package android.ijiajiao.dao.impl;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.Part;

import android.ijiajiao.dao.IdentiDao;
import android.ijiajiao.util.FileSavePathUtil;
import android.ijiajiao.util.GetDateUtil;
import android.ijiajiao.util.JDBCConnectUtil;
import android.ijiajiao.util.SaveImgUtil;

public class IdentiDaoImpl implements IdentiDao {

	@Override
	public boolean realName(String name, String path, Part parts) {
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "update identi set realname_img=? where name =?";
		boolean flag = false;
		String savePath = FileSavePathUtil.IDENTI;
		try {
			String pathlist = new SaveImgUtil().saveImg(path, parts, savePath);
			if (!"".equals(pathlist)) {
				try {
					ps = conn.prepareStatement(sql);
					ps.setString(1, pathlist);
					ps.setString(2, name);
					int row = ps.executeUpdate();
					if (row > 0) {
						flag = true;
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCConnectUtil.close(null, null, ps, conn);
		}

		return flag;
	}

	@Override
	public boolean realNameBack(String name, String path, Part parts) {
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "update identi set name_img=? where name =?";
		boolean flag = false;
		String savePath = FileSavePathUtil.IDENTI;
		try {
			String pathlist = new SaveImgUtil().saveImg(path, parts, savePath);
			if (!"".equals(pathlist)) {
				try {
					ps = conn.prepareStatement(sql);
					ps.setString(1, pathlist);
					ps.setString(2, name);
					int row = ps.executeUpdate();
					if (row > 0) {
						flag = true;
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCConnectUtil.close(null, null, ps, conn);
		}

		return flag;
	}

	@Override
	public boolean edu(String name, String path, Part parts) {
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "update identi set edu_img=? where name =?";
		boolean flag = false;
		String savePath = FileSavePathUtil.IDENTI;
		try {
			String pathlist = new SaveImgUtil().saveImg(path, parts, savePath);
			if (!"".equals(pathlist)) {
				try {
					ps = conn.prepareStatement(sql);
					ps.setString(1, pathlist);
					ps.setString(2, name);
					int row = ps.executeUpdate();
					if (row > 0) {
						flag = true;
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCConnectUtil.close(null, null, ps, conn);
		}

		return flag;
	}

	@Override
	public boolean teacher(String name, String path, Part parts) {
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "update identi set teacher_img=? where name =?";
		boolean flag = false;
		String savePath = FileSavePathUtil.IDENTI;
		try {
			String pathlist = new SaveImgUtil().saveImg(path, parts, savePath);
			if (!"".equals(pathlist)) {
				try {
					ps = conn.prepareStatement(sql);
					ps.setString(1, pathlist);
					ps.setString(2, name);
					int row = ps.executeUpdate();
					if (row > 0) {
						flag = true;
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCConnectUtil.close(null, null, ps, conn);
		}

		return flag;
	}

	@Override
	public boolean quali(String name, String path, Part parts) {
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "update identi set quali_img=? where name =?";
		boolean flag = false;
		String savePath = FileSavePathUtil.IDENTI;
		try {
			String pathlist = new SaveImgUtil().saveImg(path, parts, savePath);
			if (!"".equals(pathlist)) {
				try {
					ps = conn.prepareStatement(sql);
					ps.setString(1, pathlist);
					ps.setString(2, name);
					int row = ps.executeUpdate();
					if (row > 0) {
						flag = true;
					}
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCConnectUtil.close(null, null, ps, conn);
		}

		return flag;
	}

	@Override
	public boolean identi(String name, String summary) {
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "update identi set summary=?,time=? where name =?";
		boolean flag = false;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, summary);
			ps.setString(2, GetDateUtil.getdate());
			ps.setString(3, name);
			int row = ps.executeUpdate();
			if (row > 0) {
				flag = true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCConnectUtil.close(null, null, ps, conn);
		}

		return flag;
	}

	@Override
	public ArrayList<String> realName(String name) {
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "select realname_img from identi where name=?";
		ResultSet rs = null;
		ArrayList<String> data = new ArrayList<>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			rs = ps.executeQuery();
			while (rs.next()) {
				data.add(rs.getString(1));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCConnectUtil.close(rs, null, ps, conn);
		}

		return data;
	}

	@Override
	public ArrayList<String> realNameBack(String name) {
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "select name_img from identi where name=?";
		ResultSet rs = null;
		ArrayList<String> data = new ArrayList<>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			rs = ps.executeQuery();
			while (rs.next()) {
				data.add(rs.getString(1));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCConnectUtil.close(rs, null, ps, conn);
		}

		return data;
	}

	@Override
	public ArrayList<String> edu(String name) {
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "select edu_img from identi where name=?";
		ResultSet rs = null;
		ArrayList<String> data = new ArrayList<>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			rs = ps.executeQuery();
			while (rs.next()) {
				data.add(rs.getString(1));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCConnectUtil.close(rs, null, ps, conn);
		}

		return data;
	}

	@Override
	public ArrayList<String> teacher(String name) {
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "select teacher_img from identi where name=?";
		ResultSet rs = null;
		ArrayList<String> data = new ArrayList<>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			rs = ps.executeQuery();
			while (rs.next()) {
				data.add(rs.getString(1));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCConnectUtil.close(rs, null, ps, conn);
		}

		return data;
	}

	@Override
	public ArrayList<String> quali(String name) {
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "select quali_img from identi where name=?";
		ResultSet rs = null;
		ArrayList<String> data = new ArrayList<>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			rs = ps.executeQuery();
			while (rs.next()) {
				data.add(rs.getString(1));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCConnectUtil.close(rs, null, ps, conn);
		}

		return data;
	}

	@Override
	public ArrayList<String> identi(String name) {
		Connection conn = JDBCConnectUtil.getConnection();
		PreparedStatement ps = null;
		String sql = "select summary from identi where name=?";
		ResultSet rs = null;
		ArrayList<String> data = new ArrayList<>();
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			rs = ps.executeQuery();
			while (rs.next()) {
				data.add(rs.getString(1));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JDBCConnectUtil.close(rs, null, ps, conn);
		}

		return data;
	}

}
